<?php
/**
 * @copyright        2016 opencart.cn - All Rights Reserved
 * @link             http://www.guangdawangluo.com
 * @author           TL <mengwb@opencart.cn>
 * @created          2016-11-22 9:12:00
 * @modified         2016-11-22 9:12:00
 */

class ModelAccountOrder1 extends Model
{
    public function getOrders($start = 0, $limit = 20)
    {
        if ($start < 0) {
            $start = 0;
        }

        if ($limit < 1) {
            $limit = 1;
        }

        $query = $this->db->query("SELECT o.order_id, o.firstname, os.name as status, o.order_status_id, o.date_added, o.total, o.currency_code, o.currency_value FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_status os ON (o.order_status_id = os.order_status_id) WHERE o.customer_id = '" . (int)$this->customer->getId() . "' AND o.order_status_id > '0' AND o.store_id = '" . (int)$this->config->get('config_store_id') . "' AND os.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY o.order_id DESC LIMIT " . (int)$start . "," . (int)$limit);

        return $query->rows;
    }

    public function getOrdersOfStatusUnpaid($start = 0, $limit = 20)
    {
        if ($start < 0) {
            $start = 0;
        }

        if ($limit < 1) {
            $limit = 1;
        }

        $unpaid_status = $this->config->get('gdconfig_unpaid_status');
        $hours = $this->config->get('gdconfig_unpaid_time');
        $query = $this->db->query("SELECT o.order_id, o.firstname, o.lastname, o.shipping_firstname, o.shipping_lastname, os.name as status, o.order_status_id, o.date_added, o.total, o.currency_code, o.currency_value FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_status os ON (o.order_status_id = os.order_status_id) WHERE o.customer_id = '" . (int)$this->customer->getId() . "' AND o.order_status_id = '$unpaid_status' AND os.language_id = '" . (int)$this->config->get('config_language_id') . "' AND o.date_added > date_sub(now(),INTERVAL $hours HOUR ) ORDER BY o.order_id DESC LIMIT " . (int)$start . "," . (int)$limit);

        return $query->rows;
    }


    public function getTotalOrdersOfStatusUnpaid()
    {
        $unpaid_status = $this->config->get('gdconfig_unpaid_status');
        $hours = $this->config->get('gdconfig_unpaid_time');
        $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$this->customer->getId() . "' AND order_status_id = '$unpaid_status' AND date_added > date_sub(now(),INTERVAL $hours HOUR )");

        return $query->row['total'];
    }

    public function getOrdersOfStatusShipped($start = 0, $limit = 20)
    {
        if ($start < 0) {
            $start = 0;
        }

        if ($limit < 1) {
            $limit = 1;
        }

        $query = $this->db->query("SELECT o.order_id, o.firstname, o.lastname, o.shipping_firstname, o.shipping_lastname, os.name as status, o.order_status_id, o.date_added, o.total, o.currency_code, o.currency_value FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_status os ON (o.order_status_id = os.order_status_id) WHERE o.customer_id = '" . (int)$this->customer->getId() . "' AND o.order_status_id = '" . $this->config->get('gdconfig_shipped_status_id') . "' AND os.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY o.order_id DESC LIMIT " . (int)$start . "," . (int)$limit);

        return $query->rows;
    }

    public function getTotalOrdersOfStatusShipped()
    {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$this->customer->getId() . "' AND order_status_id = '" . $this->config->get('gdconfig_shipped_status_id') . "'");

        return $query->row['total'];
    }

    public function getOrdersOfStatusUnshipped($start = 0, $limit = 20)
    {
        if ($start < 0) {
            $start = 0;
        }

        if ($limit < 1) {
            $limit = 1;
        }

        $unshipped_status = $this->config->get('gdconfig_paid_status');//array_diff($this->config->get('config_processing_status'), array($this->config->get('config_shipped_status_id')));

        $query = $this->db->query("SELECT o.order_id, o.firstname, o.lastname, o.shipping_firstname, o.shipping_lastname, os.name as status, o.order_status_id, o.date_added, o.total, o.currency_code, o.currency_value FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_status os ON (o.order_status_id = os.order_status_id) WHERE o.customer_id = '" . (int)$this->customer->getId() . "' AND o.order_status_id in ('" . implode("','",$unshipped_status) . "') AND os.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY o.order_id DESC LIMIT " . (int)$start . "," . (int)$limit);

        return $query->rows;
    }

    public function getTotalOrdersOfStatusUnshipped()
    {
        $unshipped_status = $this->config->get('gdconfig_paid_status');//array_diff($this->config->get('config_processing_status'), array($this->config->get('config_shipped_status_id')));
        $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$this->customer->getId() . "' AND order_status_id in ('" . implode("','",$unshipped_status) . "')");

        return $query->row['total'];
    }

    public function paymentTimeout()
    {
        $unpaid_status = $this->config->get('gdconfig_unpaid_status');
        $hours = $this->config->get('gdconfig_unpaid_time');
        $cancelled_status = $this->config->get('gdconfig_cancelled_status');

        $sql = "SELECT o.order_id, o.order_status_id FROM `" . DB_PREFIX . "order` o WHERE o.order_status_id = '$unpaid_status' AND o.date_added < date_sub(now(),INTERVAL $hours HOUR )";

        $query = $this->db->query($sql);

        if ($query->rows) {
            $this->load->model('checkout/order');
            foreach ($query->rows as $unpaid_order) {
                $this->model_checkout_order->addOrderHistory($unpaid_order['order_id'], $cancelled_status, '', true);
            }
        }
    }

    public function autoCompleteOrder()
    {
        $shipped_status = $this->config->get('gdconfig_shipped_status_id');
        $days = $this->config->get('gdconfig_complete_time');
        $complete_status = $this->config->get('config_complete_status');
        $complete_status = $complete_status[0];

        $sql = "SELECT o.order_id, o.order_status_id FROM `" . DB_PREFIX . "order` o WHERE o.order_status_id = '$shipped_status' AND o.date_modified < date_sub(now(),INTERVAL $days DAY )";

        $query = $this->db->query($sql);

        if ($query->rows) {
            $this->load->model('checkout/order');
            foreach ($query->rows as $shipped_order) {
                $this->model_checkout_order->addOrderHistory($shipped_order['order_id'], $complete_status, '', true);
            }
        }
    }
}
